﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Moq;

namespace MvcWiki.Test.Repository.FileSystem.WikiNamespaceFileSystemRepository
{
    public class DeleteEntityFacts
    {
        [Fact]
        public void Call()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace" };

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            wikiNamespaceRepository.DeleteEntity(wikiNamespace);
        }

        [Fact]
        public void Checks_Whether_Namespace_Directory_Exists()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace" };

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            wikiNamespaceRepository.DeleteEntity(wikiNamespace);

            fileSystem.Verify(x => x.GetDirectories("c:\\path", "Namespace---*"));
        }

        [Fact]
        public void Throws_Exception_If_Namespace_Directory_Does_Not_Exist()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace" };

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { });

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            Assert.Throws<MvcWiki.Exceptions.NamespaceDoesNotExistException>(
                () => wikiNamespaceRepository.DeleteEntity(wikiNamespace));
        }

        [Fact]
        public void Removes_Namespace_Directory()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace" };

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            wikiNamespaceRepository.DeleteEntity(wikiNamespace);

            fileSystem.Verify(x => x.DeleteDirectory("c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc"));
        }
    }
}
